package com.wjh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjh.enums.AppHttpCodeEnum;
import com.wjh.exception.SystemException;
import com.wjh.mapper.FlowerTypeMapper;
import com.wjh.pojo.Florist;
import com.wjh.pojo.Flower;
import com.wjh.pojo.FlowerType;
import com.wjh.service.FloristService;
import com.wjh.service.FlowerService;
import com.wjh.service.FlowerTypeService;
import com.wjh.utils.ResponseResult;
import com.wjh.vo.PageVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 花的种类(FlowerType)表服务实现类
 *
 * @author makejava
 * @since 2023-10-16 19:28:02
 */
@Service("flowerTypeService")
public class FlowerTypeServiceImpl extends ServiceImpl<FlowerTypeMapper, FlowerType> implements FlowerTypeService {

    @Resource
    private FloristService floristService;

    @Resource
    private FlowerService flowerService;

    @Override
    public ResponseResult flowerType(Integer pageNum,Integer pageSize,String typeName,Integer type) {

        LambdaQueryWrapper<FlowerType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(typeName!=null,FlowerType::getName,typeName);
        queryWrapper.eq(type!=null,FlowerType::getType,type);
        //分页查询所有
        Page<FlowerType> certificatePage = new Page<>(pageNum,pageSize);
        Page<FlowerType> page = page(certificatePage, queryWrapper);
        List<FlowerType> records = page.getRecords();
        for (FlowerType record : records) {
            record.setFloristName(floristService.queryById(record.getType()).getName());
        }
        return ResponseResult.okResult(new PageVo(records,page.getTotal()));
    }

    @Override
    public ResponseResult addFlorist(FlowerType flowerType) {

        if(!StringUtils.hasText(flowerType.getName())){
            throw new SystemException(AppHttpCodeEnum.NEED_NAME);
        }

        if(flowerType.getType() == null){
            throw new SystemException(AppHttpCodeEnum.NEED_TYPE);
        }

        save(flowerType);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delFloristByIds(List<Integer> id) {
        for (Integer integer : id) {
            flowerService.delByType(integer);
        }
        LambdaQueryWrapper<FlowerType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(FlowerType::getId,id);
        remove(queryWrapper);
        return ResponseResult.okResult();
    }

    @Override
    public void delByType(List<Integer> id) {
        for (Integer typeId : id) {
            LambdaQueryWrapper<FlowerType> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(FlowerType::getType,typeId);
            List<FlowerType> list = list(queryWrapper);
            for (FlowerType flowerType : list) {
                flowerService.delByType(flowerType.getId());
            }
            remove(queryWrapper);
        }
    }

}
